// 导入数据库
const dbModel = require('../lib/db.js')

// 新建walls
exports.insterWall = async (req,res) => {
    let data = req.body;
    
    await dbModel.insterWall([data.type, data.message, data.name, data.userId, data.moment, data.label, data.color, data.src]).then(result => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 新建反馈
exports.insterFeedback = async (req,res) => {
    let data = req.body;

    await dbModel.insterFeedback([data.wallId, data.userId, data.type, data.moment]).then(result => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 新建评论
exports.insterComment = async (req,res) => {
    let data = req.body;

    await dbModel.insertComment([data.wallId,data.userId, data.src, data.moment, data.comment, data.name]).then(result => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 删除墙，主表对应多条子表一起删除
exports.deleteWall = async (req,res) => {
    let data = req.body;

    if(data.src) {
        Mkdir.delFile('data/' + data.src)
    }
    await dbModel.deleteWall(data.id).then((result) => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 删除反馈
exports.deleteFeedback = async (req,res) => {
    let data = req.body;

    await dbModel.deleteFeedback(data.id).then((result) => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 删除评论
exports.deleteComment = async(req,res) => {
    let data = req.body;

    await dbModel.deleteComment(data.id).then((result) => {
        res.send({
            code: 200,
            message: result
        })
    })
}

// 分页查询
exports.findWallPage = async (req,res) => {
    let data = req.body;

    await dbModel.findWallPage(data.page, data.pagesize, data.type, data.label).then(async result => {
        for(let i = 0; i < result.length; i++) {
            // 喜欢
            result[i].like = await dbModel.feedbackCount(result[i].id,0)
            // 举报
            result[i].report = await dbModel.feedbackCount(result[i].id,1)
            // 撤销
            result[i].cancel = await dbModel.feedbackCount(result[i].id,2)
            // 是否点赞
            result[i].isLike = await dbModel.likeCount(result[i].id, data.userId)
            // 评论数
            result[i].commentCount = await dbModel.commentCount(result[i].id)
        }
        res.send({
            code : 200,
            message : result
        })
    })
}

// 查询评论
exports.findCommentPage = async (req,res) => {
    let data = req.body;

    await dbModel.findCommentPage(data.page, data.pagesize, data.id).then(result => {
        res.send({
            code: 200,
            message: result
        })
    })
}